package com.gupao.javavip.netty.push.server;

import com.gupao.javavip.netty.push.protocol.AbstractPushChannelInitializer;
import com.gupao.javavip.netty.push.protocol.RequestDecoder;
import com.gupao.javavip.netty.push.protocol.ResponseEncoder;
import com.gupao.javavip.netty.push.server.processor.handler.PushMsgServerHandler;
import com.gupao.javavip.netty.push.server.processor.handler.ReceiveMsgServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.apachecommons.CommonsLog;

/**
 * 基于Netty的推送系统
 */
@CommonsLog
public class PushServer {
    private int port = 8080;

    public PushServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) {
        new PushServer(8080).start();
    }

    private void start() {
        //Netty封装了NIO，Reactor模型，Boss，worker
        // Boss线程
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        // Worker线程
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            // Netty服务
            //ServerBootstrap   ServerSocketChannel
            ServerBootstrap server = new ServerBootstrap();
            // 链路式编程
            server.group(bossGroup, workerGroup)
                    // 主线程处理类,看到这样的写法，底层就是用反射
                    .channel(NioServerSocketChannel.class)
                    // 针对主线程的配置 分配线程最大数量 1024
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    // 针对子线程的配置 保持长连接
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    // 子线程处理类 , Handler
                    .childHandler(new AbstractPushChannelInitializer(true) {
                        // 客户端初始化处理
                        @Override
                        protected void doInitChannel(SocketChannel ch) {
                            log.debug("Handler初始化处理");

                            //Inbound
                            ch.pipeline().addLast("request-decoder", new RequestDecoder());
                            ch.pipeline().addLast("push-msg-server-handler", new PushMsgServerHandler());
                            ch.pipeline().addLast("receive-msg-server-handler", new ReceiveMsgServerHandler());

                            //Outbound
                            ch.pipeline().addLast("response-encoder", new ResponseEncoder());
                        }
                    });

            // 启动服务器
            ChannelFuture f = server.bind(port).sync();
            log.info("推送平台已启动，监听的端口是：" + port);
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            // 关闭线程池
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }


}
